#!/bin/sh

# Author : Jagadish Manchala
# If You Have any Issues Executing the Scripts , Dont Look For Me :-)


# description: Control Script For Starting , Stopping and Restarting the OEM
# processname: java
# pidfile: /var/run/oem-agent.pid

# The Start,Stop and Recycle Of OEM also Include Oms Agent.So If we restart,start or stop oem we need to make sure that OMS is handled before the OEM.



# ----------------------------------------------------------------------
# Directory Locations Necessary For Execution of the Commands
# ----------------------------------------------------------------------
OEM_INSTANCE_DIR="${OEM_CFGDIR}/oms"
AGENT_PIDFILE_DIR="${OEM_SCRIPTSDIR}/bin"

# -------------------------------
# Determine where this script is, and change to its directory

_THIS_SCRIPT="${OEM_INSTANCE_DIR}/bin/emctl"

# ----------------------------------------------------------------------
# Function that prepares the location where the pidfile will live
# ----------------------------------------------------------------------


prepare_pid_dir ()
{
   mkdir -p "$AGENT_PIDFILE_DIR"

   if [ "$?" != "0" ]; then
      echo "Cannot create the directory where the pidfile will go: ${AGENT_PIDFILE_DIR}"
      exit 1
   fi

}

# ----------------------------------------------------------------------
# Function that ensures that the pidfile no longer exists
# ----------------------------------------------------------------------

remove_pid_files ()
{
   if [ -f "${OMS_PIDFILE}" ]; then
      rm "${OMS_PIDFILE}"
   fi

   if [ -f "${OEM_PIDFILE}" ]; then
      rm "${OEM_PIDFILE}"
   fi

}


remove_pid_oem ()
{

   if [ -f "${OEM_PIDFILE}" ]; then
      rm "${OEM_PIDFILE}"
   fi

}



# ----------------------------------------------------------------------
# Function that sets _STATUS, _RUNNING and _PID based on the status of
# the RHQ Agent process as found in the pidfile
# ----------------------------------------------------------------------

check_status ()
{
  
    #Grep the PID By EMGC_OMS1
    OEM_PID=$(pgrep -n -f Dweblogic.Name=EMGC_OMS1)

    #If Pid Exists , Echo "Already Running"
	if [[ $OEM_PID != "" ]]; then
            OEM_STATUS="OEM  (pid $OEM_PID) is running"
            OEM_RUNNING=1
     else
            OEM_STATUS="OEM (pid $OEM_PID) is NOT running"
            OEM_RUNNING=0
     fi

}


check_status_of_pid ()
{

    if [ -n "$1" ] && kill -0 $1 2>/dev/null ; then
       OEM_STATUS="OEM Agent (pid $1) is running"
       OEM_RUNNING=1
    else
       OEM_STATUS="OEM Agent (pid $1) is NOT running"
       OEM_RUNNING=0
    fi

}


# -------------------------------
# Find out where to put the pidfile and prepare its directory
#--------------------------------

OEM_PIDFILE="${AGENT_PIDFILE_DIR}/oem-agent.pid"
touch $OEM_PIDFILE

# --------------------------------------------------------------------------
# Main Processing Starts here
# check_status Is Executed First To get the Current Stats
#---------------------------------------------------------------------------


check_status

case "$1" in
'start')
   prepare_pid_dir
 
    echo "Starting the Agent Now"

     #Check For the Status Of OMS
     if [ "$OEM_RUNNING" = "1" ]; then
           echo $OEM_STATUS
           exit 0
        fi

     OEM_AGENT_IN_BACKGROUND="${OEM_PIDFILE}"

     echo "Starting OEM Agent..."

 
     #Determine the command to execute when starting the agent
       if [ -z "$OEM_AGENT_START_COMMAND" ]; then

          _START_SCRIPT="${OEM_INSTANCE_DIR}/bin/emctl"

         if [ ! -f "$_START_SCRIPT" ]; then
             echo "ERROR! Cannot find the OMS Agent start script"
             echo "Not found: $_START_SCRIPT"
             exit 1
       fi

      OEM_AGENT_START_COMMAND="${_START_SCRIPT} start oms"
      fi

      echo "Script Getting Executed is $OEM_AGENT_START_COMMAND"

      # Execute the Script To Start the agent now!
      . $OEM_AGENT_START_COMMAND > "/logs/oem/oem.log" 2>&1 &

      sleep 2m

      OEM_AGENT_BACKGROUND_STRING=$($OEM_INSTANCE_DIR/bin/emctl status agent | grep 'Oracle Management Server is Up')

        if [[ $OEM_AGENT_BACKGROUND_STRING  -eq 0 ]] ; then  # we found a not
               OEM_AGENT_BACKGROUND_PID=$(pgrep -n -f Dweblogic.Name=EMGC_OMS1)
	       echo $OEM_AGENT_BACKGROUND_PID > "$OEM_AGENT_IN_BACKGROUND"
	
	fi


      check_status
      echo $OEM_STATUS

      if [ "$OEM_RUNNING" = "1" ]; then
         exit 0
      else
         echo "Failed to start - make sure the OEM is fully configured properly"
         exit 1
      fi
  
   
;;

# ----------------------- Start Function Complete ------------------------
'stop')

   prepare_pid_dir

  if [ "$OEM_RUNNING" = "0" ]; then
           echo $OEM_STATUS
           remove_pid_oem
           exit 0
        fi

        echo "Stopping OEM Agent..."

	_PID_TO_KILL=$OEM_PID

        echo "OEM Agent (pid=$OEM_PID) is stopping..."

        #Determine the command to execute when starting the agent
       if [ -z "$OEM_AGENT_STOP_COMMAND" ]; then
          _STOP_SCRIPT="${OEM_INSTANCE_DIR}/bin/emctl"

         if [ ! -f "$_STOP_SCRIPT" ]; then
             echo "ERROR! Cannot find the OMS Agent stop script"
             echo "Not found: $_STOP_SCRIPT"
             exit 1
       fi


      OEM_AGENT_STOP_COMMAND="${_STOP_SCRIPT} stop oms"
      fi

      echo "Script Getting Executed is $OEM_AGENT_STOP_COMMAND"

      # Execute the Script To Start the agent now!
      . $OEM_AGENT_STOP_COMMAND > "/logs/oem/oem.log" 2>&1 &

      sleep 2m

      OEM_AGENT_BACKGROUND_STRING=$($OEM_INSTANCE_DIR/bin/emctl status agent | grep 'Oracle Management Server is Down')

      if [[ $OEM_AGENT_BACKGROUND_STRING  -eq 0 ]] ; then  # we found a not
            remove_pid_oem
    	    echo "OEM Stopped Successfully";
            exit 0
      fi

      check_status_of_pid $_PID_TO_KILL

      if [ "$OEM_RUNNING" = "1" ]; then
          kill -9 $_PID_TO_KILL
      fi
   
      while [ "$OEM_RUNNING" = "1" ]; do
          sleep 2
          check_status_of_pid $_PID_TO_KILL
       done

       remove_pid_oem
       echo "OEM Agent has stopped."
       exit 0

;;

#------------------------ Stop Function Complete ------------------------
'status')

         echo $OEM_STATUS
        if [ "$OEM_RUNNING" = "0" ]; then
           exit 1
        fi
        exit 0
 

;;
#------------------------ Status Function Complete ----------------------
'restart')


        echo -e "Stopping the OEM..."
        "${_THIS_SCRIPT}" stop oms > /dev/null 2>&1
        sleep 1m
        echo -e "OEM Stopped Sucessfully"

        echo -e "Starting the OEM Now ..."
        "${_THIS_SCRIPT}" start oms > /dev/null 2>&1
        sleep 2m

        OEM_AGENT_IN_BACKGROUND="${OEM_PIDFILE}"
        export OEM_AGENT_IN_BACKGROUND

        OEM_AGENT_BACKGROUND_STRING=$($OEM_INSTANCE_DIR/bin/emctl status agent | grep 'Oracle Management Server is Up')

        if [[ $OEM_AGENT_BACKGROUND_STRING  -eq 0 ]] ; then  # we found a not
               OEM_AGENT_BACKGROUND_PID=$(pgrep -n -f Dweblogic.Name=EMGC_OMS1)
               echo $OEM_AGENT_BACKGROUND_PID > "$OEM_AGENT_IN_BACKGROUND"

        fi
   
       remove_pid_oem

       if [ "$OEM_AGENT_IN_BACKGROUND" != "nofile" ]; then
           echo $OEM_AGENT_BACKGROUND_PID > "$OEM_AGENT_IN_BACKGROUND"
       fi

        echo -e "Oem Started Succssfully...."
        exit $?


        ;;

#-----------------------  Restart Function Complete --------------------
*)
        echo "Usage: $0 { start | stop | restart | status }"
        exit 1
        ;;
esac


